﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL.Util;
using DTO;
using BLL;

namespace MZ.Money
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCard();
                BindCategory();
                BindList();
            }
        }
        /// <summary>
        /// 绑定记录列表
        /// </summary>
        private void BindList()
        {
            RptList.DataSource = RecordBLL.GetMyRecordTop10(User.Identity.Name);
            RptList.DataBind();
        }


        /// <summary>
        /// 绑定消费账户
        /// </summary>
        private void BindCard()
        {
            DdlMyCard.DataSource = CacheHelper.GetMyCard(User.Identity.Name);
            DdlMyCard.DataBind();
        }

        private void BindCategory()
        {
            var myCategory = CacheHelper.GetMyAllCategory(User.Identity.Name);
            DdlMyCategory.DataSource = myCategory;
            DdlMyCategory.DataBind();
        }

        protected void DdlMyCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            DdlMySubCategory.Visible = true;
            var myCategory = CacheHelper.GetMyAllCategory(User.Identity.Name);
            var sub = myCategory.Find(f => f.ID == int.Parse(DdlMyCategory.SelectedValue));
            DdlMySubCategory.DataSource = sub.Nodes;
            DdlMySubCategory.DataBind();
        }

        protected void BtnMoneySubmit_Click(object sender, EventArgs e)
        {
            AddRecord();
        }

        protected void BtnAutoSave_Click(object sender, EventArgs e)
        {
            var record = AddRecord();
            AutoSetting auto = new AutoSetting();
            auto.Model = record;
            auto.LastTime = DateTime.Now;
            auto.AID = int.Parse(User.Identity.Name);
            auto.AutoType = int.Parse(DdlAutoType.SelectedValue);
            AutoSettingBLL.AddAutoSetting(auto);
        }

        #region display text
        protected String GetCardText(object cid)
        {
            return CardBLL.GetMyCard(User.Identity.Name, cid.ToString()).Title;
        }

        protected string GetCategoryText(object cid)
        {
            return CategoryBLL.GetCategory(User.Identity.Name, int.Parse(cid.ToString())).Title;
        }

        protected string GetDateString(object date)
        {
            var d = DateTime.Parse(date.ToString());
            return string.Format("{0}年{1}月{2}日", d.Year, d.Month, d.Day);
        }
        #endregion

        private Record AddRecord()
        {
            Record r = new Record()
            {
                AccountID = int.Parse(User.Identity.Name),
                CardID = int.Parse(DdlMyCard.SelectedValue),
                CreateTime = DateTime.Parse(TxtRecordTime.Text),
                CategoryID = int.Parse(DdlMySubCategory.SelectedValue),
                Money = decimal.Parse(TxtMoney.Text),
                Remark = TxtRemark.Text
            };
            RecordBLL.AddRecord(r);
            BindList();
            return r;
        }
    }
}
